package com.sczmgk.hotline_pzh.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements OnPageChangeListener, OnClickListener {
    private final String TAG = "GuideActivity";
    private List<ImageView> imageViewList;
    private int basicWidth = -1;
    /**
     * 装小点的容器
     */
    private LinearLayout llPointGroup;
    /**
     * 选中的小点
     */
    private View mSelectedPoint;
    /**
     * 开始体验的按钮
     */
    private Button btnStartExperience;
    /**
     * 小点的尺寸
     */
    private int pointSize;
    private ViewPager mViewPager;
    private GuideAdapter mAdapter;
    private int[] imageIDs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);

        initView();
    }

    @SuppressLint("NewApi")
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager_guide);
        btnStartExperience = (Button) findViewById(R.id.btn_guide_start_experience);
        llPointGroup = (LinearLayout) findViewById(R.id.ll_guide_point_group);
        mSelectedPoint = findViewById(R.id.v_guide_selected_point);
        /**
         * 引导图片
         */
        imageViewList = new ArrayList<ImageView>();
        imageIDs = new int[]{
                R.drawable.guide_1_1280x720,
                R.drawable.guide_2_1280x720,
                R.drawable.guide_3_1280x720,
                R.drawable.guide_4_1280x720
        };

        ImageView iv;
        View view;
        //取得像素密度
        float screenDensity = CommonUtils.getScreenDensity(GuideActivity.this);
        pointSize = (int) (10 * screenDensity);
        //初始化
        for (int i = 0; i < imageIDs.length; i++) {
            iv = new ImageView(this);
//		    iv.setBackground(getResources().getDrawable(imageIDs[i]));
//			iv.setBackgroundResource(imageIDs[i]);
            //加入引导图片
            imageViewList.add(iv);
            //实例化小点
            view = new View(this);

            LayoutParams params = new LayoutParams(pointSize, pointSize);
            if (i != 0) {
                //设置第一个点后面的 maginLeft
                params.leftMargin = pointSize;
            }
            //设置小点参数
            view.setLayoutParams(params);
            /**
             * 设置小点背景
             */
            view.setBackgroundResource(R.drawable.guide_point_gray_bg);
            /**
             * 加入小点
             */
            llPointGroup.addView(view);
        }
        mAdapter = new GuideAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(this);
        btnStartExperience.setOnClickListener(this);
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViewList.get(position));
        }


        public Object instantiateItem(ViewGroup container, int position) {            //在此设置背景图片，提高加载速度，解决OOM问题
            View view = imageViewList.get(position);
            int count = getCount();
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            view.setBackgroundResource(imageIDs[position % count]);
            view.setLayoutParams(params);
            container.addView(view, 0);
            return imageViewList.get(position);
        }


//		@Override
//		public Object instantiateItem(ViewGroup container, int position) {
//			ImageView imageView = imageViewList.get(position);
//			container.addView(imageView);
//			return imageView;
//		}
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {
        //移动小点
        if (basicWidth == -1 || basicWidth ==0 ) {
            //取得第二个小点的与第一个小点的距离
            basicWidth = llPointGroup.getChildAt(1).getLeft() - llPointGroup.getChildAt(0).getLeft();
        }
        int leftMargin = (int) ((position + positionOffset) * basicWidth);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(pointSize, pointSize);
        params.leftMargin = leftMargin;
        mSelectedPoint.setLayoutParams(params);
//        MyLog.d(TAG,"leftMargin : "+ leftMargin);
//        MyLog.d(TAG,"baseWidth : "+ basicWidth);
        if(leftMargin > basicWidth*2){
            llPointGroup.setVisibility(View.GONE);
            mSelectedPoint.setVisibility(View.GONE);
        }else{
            mSelectedPoint.setVisibility(View.VISIBLE);
            llPointGroup.setVisibility(View.VISIBLE);
        }
        //一. 1.如果是第三页  2.如果是向右滑   隐藏小点容器
        //二. 1.如果小于等于三页,显示小点容器
        //
    }

    @Override
    public void onPageSelected(int position) {
        if (position == imageViewList.size() - 1) {
            btnStartExperience.setVisibility(View.VISIBLE);
        } else {
            btnStartExperience.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        CacheUtils.cacheBooleanData(this, Constants.IS_OPEN_MAIN_PAGE_KEY, true);

        startActivity(new Intent(this, MainActivity.class));
        for (int i = 0; i < imageViewList.size(); i++) {

            imageViewList.get(i).destroyDrawingCache();
            mViewPager.destroyDrawingCache();

        }

        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁所有ImageView,及所有图片
        for (ImageView v : imageViewList) {
            Drawable drawable = v.getDrawable();
            if (drawable != null && drawable instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                if (bitmap != null) {
                    bitmap.recycle();
                }
            }
        }
        imageViewList.clear();
    }

}
